/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

	
	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

 ****/
package org.activequant.core.util;

import org.activequant.core.types.TimeStamp;

/**
 * Static helper class with assertions and checks. <br/>
 * <br>
 * <b>History:</b><br>
 * - [May 31, 2008] Created (mark)<br>
 * 
 * @author mark
 */
public class Check {

	/**
	 * Checks if an object is not null. If the check fails an <code>AssertionError</code> is thrown.<br/>
	 * If the given object(Object) is null, it throws an AssertionError with the "${label} is null" message.
	 * @param label
	 *            the name of the object.
	 * @param object
	 *            the object to check.
	 */
	public static void notNull(final String label, final Object object) {
		if (object == null) {
			assertion(label + " is null");
		}
	}

	/**
	 * Checks if two objects are not null and equal. If the check fails an <code>AssertionError</code> is thrown.<br/>
	 * The given object1(Object) and object2(Object) must be equal and not null. Otherwise it throws an AssertionError
	 * with the appropriate message using the given label1(String) and label2(String).
	 * @param label1
	 *            the name of the first object.
	 * @param label2
	 *            the name of the second object.
	 * @param object1
	 *            the first object to check.
	 * @param object2
	 *            the second object to check.
	 */
	public static void notNullAndEqual(final String label1, final String label2, final Object object1, final Object object2) {
		notNull(label1, object1);
		notNull(label2, object2);

		if (!object1.equals(object2)) {
			assertion(label1 + " is not equal to " + label2);
		}
	}

	/**
	 * Checks if a integer value is at least a minimum value. If the check fails an <code>AssertionError</code> is thrown.<br/>
	 * If the given value(int) is less than the given minimum(int), it throws an AssertionError with the "${label} is ${value}" message.
	 * @param label
	 *            the name of the value.
	 * @param minimum
	 *            the minimum of the value.
	 * @param value
	 *            the value to check.
	 */
	public static void atLeast(final String label, final int minimum, final int value) {
		if (value < minimum) {
			assertion(label + " is " + value);
		}
	}

	/**
	 * Checks if a long value is at least a minimum value. If the check fails an <code>AssertionError</code> is thrown.<br/>
	 * If the given value(long) is less than the given minimum(long), it throws an AssertionError with the "${label} is ${value}" message.
	 * @param label
	 *            the name of the value.
	 * @param minimum
	 *            the minimum of the value.
	 * @param value
	 *            the value to check.
	 */
	public static void atLeast(final String label, final long minimum, final long value) {
		if (value < minimum) {
			assertion(label + " is " + value);
		}
	}

	/**
	 * Checks if a value is at most a maximum value. If the check fails an <code>AssertionError</code> is thrown.<br/>
	 * If the given value(int) is greater than the given maximum(int), it throws an AssertionError with the "${label} is ${value}" message.
	 * @param label
	 *            the name of the value.
	 * @param maximum
	 *            the maximum of the value.
	 * @param value
	 *            the value to check.
	 */
	public static void atMost(final String label, final int maximum, final int value) {
		if (value > maximum) {
			assertion(label + " is " + value);
		}
	}

	/**
	 * Checks if timeStamp1 and timeStamp2 are not null, and if timeStamp2 is equal to or after timeStamp1.<br/>
	 * Neither of the given timeStamp1(TimeStamp) and timeStamp2(TimeStamp) can be null, and timeStamp2(TimeStamp) can not be before timeStamp1(TimeStamp).
	 * Otherwise it throws an AssertionError with the appropriate message using the given label1(String) and label2(String).
	 * @param label1
	 *            the name of timeStamp1
	 * @param label2
	 *            the name of timeStamp2
	 * @param timeStamp1
	 *            the time stamp to check against
	 * @param timeStamp2
	 *            the time stamp to check
	 */
	public static void isNotNullAndEqualOrAfter(final String label1, final String label2, final TimeStamp timeStamp1, final TimeStamp timeStamp2) {
		notNull(label1, timeStamp1);
		notNull(label2, timeStamp2);

		if (timeStamp2.isBefore(timeStamp1)) {
			assertion(label2 + "is not equal or after" + label1);
		}
	}

	/************************************************************************************
	 * Private methods
	 ************************************************************************************/
	private static void assertion(final String string) {
		throw new AssertionError(string);
	}

}
